package com.basic.business.declineWarnBoard.controller;

import com.basic.business.declineBacklog.service.ITDeclineBacklogService;
import com.basic.business.declineWarnBoard.vo.*;
import com.basic.business.goodsIndicators.service.ITGoodsIndicatorsService;
import com.basic.common.annotation.FileExport;
import com.basic.common.core.controller.BaseController;
import com.basic.common.core.domain.AjaxResult;
import com.basic.common.core.page.TableDataInfo;
import com.basic.common.utils.easyExcel.EasyExcelUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * 下滑预警看板控制器
 */
@RestController
@RequestMapping("/business/declineWarnBoard")
public class TDeclineWarnBoardController extends BaseController {


    @Resource
    private ITGoodsIndicatorsService tGoodsIndicatorsService;

    @Resource
    private ITDeclineBacklogService tDeclineBacklogService;

    /**
     * 获取下滑预警看板数据
     * @param declineWarnBoardReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:declineWarnBoard:list')")
    @GetMapping("/getDeclineWarnBoardData")
    public TableDataInfo getDeclineWarnBoardData(DeclineWarnBoardReqVo declineWarnBoardReqVo) {
        startPage();
        List<DeclineWarnBoardRespVo> list = tGoodsIndicatorsService.getDeclineWarnBoardData(declineWarnBoardReqVo);
        for (DeclineWarnBoardRespVo declineWarnBoardRespVo : list){
            int count = tDeclineBacklogService.getBackLogCountByGoodsId(declineWarnBoardRespVo.getGoodsId());
            declineWarnBoardRespVo.setBackLogCount(count);
        }
        return getDataTable(list);
    }


    /**
     * 获取下滑预警待办复盘数据
     * @param declineReplayPageReqVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:declineReplay:list')")
    @GetMapping("/declineReplayData")
    public TableDataInfo declineReplayData(DeclineReplayPageReqVo declineReplayPageReqVo) {
        startPage();

        //已优化记录
        List<DeclineReplayRespVo> optimizeList = tDeclineBacklogService.getOptimizeList(declineReplayPageReqVo);

        return getDataTable(optimizeList);
    }

    /**
     * 获取下滑预警复盘汇总数据
     * @param declineReplayPageReqVo
     * @return
     */
    @GetMapping("/getReplayCollectData")
    @PreAuthorize("@ss.hasPermi('business:declineReplay:collectData')")
    public AjaxResult getReplayCollectData(DeclineReplayPageReqVo declineReplayPageReqVo) {
        DeclineReplayCollectRespVo collectData = tDeclineBacklogService.getDeclineManageCollectData(declineReplayPageReqVo);

        return success(collectData);
    }

    /**
     * 导出下滑预警复盘数据
     * @param declineReplayPageReqVo
     * @return
     * @throws IOException
     */
    @PreAuthorize("@ss.hasPermi('business:declineReplay:export')")
    @FileExport(fileName = "下滑预警复盘数据.xlsx")
    @PostMapping("/exportDeclineReplayData")
    public String exportLinkReplayData(@RequestBody DeclineReplayPageReqVo declineReplayPageReqVo) throws IOException {

        List<DeclineReplayRespVo> list = tDeclineBacklogService.getOptimizeList(declineReplayPageReqVo);

        return EasyExcelUtils.excelExport(DeclineReplayRespVo.class,"下滑预警复盘数据", list);
    }
}
